#!/bin/sh

# this is more strick checking
# 1. count for each table
# 2. N record md5sum
# 3. each line md5sum
# 
# therefore, we need ms replication.
# if diff, then apply table.diff records manually
# 1. miss -> insert
# 2. diff -> upate or 
# TODO: automate
# TODO: slave check if nslave_record_count > nmaster_record_count

#my065043.cm4 my065058.cm4

user='dump'
passwd='dump123'
port='3306'
slave_db_name="cm5_zds"
slave_host="172.25.89.28"
ndiff=0
n_check_interval=1000
mlog=mlog
slog=slog
function my_echo {
    local str=$1
    echo "${str}"
}

function max_value {
    local v1=$1
    local v2=$2
    local max=$1
    if (($max < $2)) ;then
        max=$2
     fi
    echo $max
}


function check_detail {
    local host=$1
    local db=$2
    local tbl=$3
    local nrow=$4
    local pk=$5
    local cmd=
    local cmd2=
    local ret=
    local ret2=
    local cur_row=0
    #nrow=10

    dlog="${tbl}.diff"
    echo `date` > $dlog
    while (($cur_row < $nrow))
    do
        if [ ${cur_row} -ge ${n_check_interval}  -a $((${cur_row}%${n_check_interval})) -eq 0 ] ;then
            echo "checking $cur_row of $nrow for ${host}:${db}:${tbl} $ndiff diffs ... "
        fi

        cmd1="select * from ${db}.${tbl} limit $cur_row,$n_check_interval;"
        mysql -h${host} -u${user} -p${passwd} -P${port} -e "$cmd1" > $mlog
        cmd2="select * from ${slave_db_name}.${tbl} limit $cur_row,$n_check_interval;"
        mysql -h${slave_host} -u${user} -p${passwd} -P${port} -e "$cmd2" > $slog
        md1=`md5sum $mlog|awk '{print $1}'`
        md2=`md5sum $slog|awk '{print $1}'`
    	if [ "${md1}" == "${md2}" ];then
            continue;
        fi

        first_line=1
        while read line
         do
        if [ $first_line -eq 1 ];then
    	    first_line=0
	        continue
        fi
        value=`echo $line | awk '{print $1}'`
        cmd="select * from ${slave_db_name}.${tbl} where $pk=$value"
        ret=`mysql -h${slave_host} -u${user} -p${passwd} -P${port} -e "$cmd" | tail -n 1`
        if [ "X$ret" == "X" ] ;then
        #more strict checking mode
        #if [ "`echo $line|md5sum`" != "`echo $ret|md5sum`" ] ;then
            echo "$line" | tee -a $dlog
            echo "$ret" | tee -a $dlog
            echo "" | tee -a $dlog
    
            let ndiff=ndiff+1
        fi
        done < "${mlog}"
        
        let cur_row=cur_row+n_check_interval
    done
}


function check_consistent {
    local nhosts=$1
    local ndbs=$2
    local ntbls=$3
    local pk=$4
    local flag=$5
    local cmd=
    local cmd2=
    local ret=
    local ret2=

    my_echo "checking thread $thread ..."
    local nhost_idx
    local ndb_idx
    local ntbl_idx
    let nhost_idx=nhosts-1
    let ndb_idx=ndbs-1
    let ntbl_idx=ntbls-1


    for master_index in `seq 0 $nhost_idx`
    do
        local master_id=$((${master_index}+43))
        #local master_name="my0650${master_id}.cm4"
        local master_name="172.23.112.87"

        for db_index in `seq 0 $ndb_idx`
        do
            local cur_db_index=$((${master_index} * 2 + ${db_index}))

            #local db_name=`printf feel_%02d ${cur_db_index}`
            local db_name="zdstest"

            for tbl_index in `seq 0 $ntbl_idx`
            do
                local cur_tbl_index=$((${cur_db_index} * 4 + ${tbl_index}))
                local tbl_name=`printf "auction_tag_%03d" ${tbl_index}`
                cmd1="select count(*) from ${db_name}.${tbl_name};"
                ret1=`mysql -h${master_name} -u${user} -p${passwd} -P${port} -e "$cmd1" | tail -n1`
                my_echo "${master_name} : ${cmd1} : ${ret1}"

                cmd2="select count(*) from ${slave_db_name}.${tbl_name};"
                ret2=`mysql -h${slave_host} -u${user} -p${passwd} -P${port} -e "$cmd2" | tail -n1`
                my_echo "${slave_host} : ${cmd2} : ${ret2}"

                if ((${ret1} != ${ret2})) ;then
                    echo "master-slave are not same!!!"
                    if [ "X$flag" == "XFORCE" ] ;then
                        check_detail ${master_name} ${db_name} ${tbl_name} $(max_value $ret $ret2)  $pk &
                    fi    
                fi
            done

        done
    done
}

# usage:  sh $0 NHOSTS NDATABASES NTABLES
check_consistent 1 1 32 auction_id
#check_consistent 1 1 1 id FORCE
#check_consistent 16 2 4 FORCE 
#check_consistent 16 2 4 
wait
echo "Finish check data! ndiff=$ndiff"

